package com.jilefojing.web;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.Statistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBook;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBookChapter;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.service.IEBookChapterService;
import com.jilefojing.service.IEBookService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.web.base.BaseWeb;

@Controller
@RequestMapping("book")
public class BookWeb extends BaseWeb {

	@Autowired
	private IEBookService eBookService;
	@Autowired
	private IEBookChapterService eBookChapterService;
	@Autowired
	private IStatisticsService statisticsService;

	@RequestMapping("qldzj/index.html")
	public String toQianLongDaZangJingPage(HttpServletRequest request) {
		EnhancedEBook qldaj_enhancedEBook = new EnhancedEBook();
		qldaj_enhancedEBook.setId(1L);
		qldaj_enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(qldaj_enhancedEBook, null);

		request.setAttribute("catelog_eBooks", qldaj_enhancedEBook.getEnhancedChildren());

		return "web/book/qianlongdazangjing/index";
	}

	@RequestMapping("qldzj/catalog-{id}.html")
	public String toQianLongDaZangJingCatalogPage(@PathVariable("id") long id, HttpServletRequest request) {
		// 目录
		EnhancedEBook qldaj_enhancedEBook = new EnhancedEBook();
		qldaj_enhancedEBook.setId(1L);
		qldaj_enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(qldaj_enhancedEBook, null);

		request.setAttribute("catelog_eBooks", qldaj_enhancedEBook.getEnhancedChildren());

		EnhancedEBook enhancedEBook = (EnhancedEBook) eBookService.getEnhanced(id, null);
		enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(enhancedEBook, null);

		request.setAttribute("eBook", enhancedEBook);
		request.setAttribute("eBooks", enhancedEBook.getEnhancedChildren());

		statisticsService.updateCount(ItemTypeEnumForSource.BOOK, id, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

		return "web/book/qianlongdazangjing/catalog";
	}

	@RequestMapping("qldzj/catalog-{paretnId}/{id}.html")
	public String toQianLongDaZangJingSubCatalog_Page(@PathVariable("paretnId") long paretnId, @PathVariable("id") long id, HttpServletRequest request) {
		// 目录
		EnhancedEBook qldaj_enhancedEBook = new EnhancedEBook();
		qldaj_enhancedEBook.setId(1L);
		qldaj_enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(qldaj_enhancedEBook, null);

		request.setAttribute("catelog_eBooks", qldaj_enhancedEBook.getEnhancedChildren());

		EnhancedEBook enhancedParent = (EnhancedEBook) eBookService.getEnhanced(paretnId, null);
		request.setAttribute("parent", enhancedParent);

		EnhancedEBook enhancedEBook = (EnhancedEBook) eBookService.getEnhanced(id, null);

		Pagination<EnhancedEBookChapter> pagination = new Pagination<EnhancedEBookChapter>(1, Integer.MAX_VALUE, true);
		pagination = eBookChapterService.getPagination(id, pagination, null);

		request.setAttribute("eBook", enhancedEBook);
		request.setAttribute("eBookChapterpagination", pagination);

		statisticsService.updateCount(ItemTypeEnumForSource.BOOK, id, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

		return "web/book/qianlongdazangjing/sub_catalog";
	}

	@RequestMapping("chapter/{id}.html")
	public String toChapterInfoPage(@PathVariable("id") long id) {
		EnhancedEBookChapter enhancedEBookChapter = (EnhancedEBookChapter) eBookChapterService.getEnhanced(id, null);
		EnhancedEBook parentEnhancedEBook = enhancedEBookChapter.getEnhancedEBook();
		parentEnhancedEBook = (EnhancedEBook) eBookService.getEnhanced(parentEnhancedEBook.getId(), null);

		return super.redirect("/book/qldzj/catalog-" + parentEnhancedEBook.getEnhancedParent().getId() + "/" + parentEnhancedEBook.getId() + "/" + id + ".html");
	}

	@RequestMapping("qldzj/catalog-{topId}/{paretnId}/{chapterId}.html")
	public String toQianLongDaZangJingSubInfoPage(@PathVariable("topId") long topId, @PathVariable("paretnId") long paretnId, @PathVariable("chapterId") long chapterId, HttpServletRequest request) {

		EnhancedEBook enhancedTop = (EnhancedEBook) eBookService.getEnhanced(topId, null);
		request.setAttribute("top", enhancedTop);

		EnhancedEBook enhancedParent = (EnhancedEBook) eBookService.getEnhanced(paretnId, null);
		request.setAttribute("parent", enhancedParent);

		EnhancedEBookChapter enhancedEBookChapter = (EnhancedEBookChapter) eBookChapterService.getEnhanced(chapterId, null);
		request.setAttribute("chapter", enhancedEBookChapter);
		request.setAttribute("description", Jsoup.parse(enhancedEBookChapter.getContent()).text().replaceAll(">", "").replaceAll("<", ""));// 用于网站简介

		EnhancedEBookChapter enhancedPrevEBookChapter = eBookChapterService.getEnhancedPrevInEBook(paretnId, chapterId, null);
		request.setAttribute("prevChapter", enhancedPrevEBookChapter);

		EnhancedEBookChapter enhancedNextEBookChapter = eBookChapterService.getEnhancedNextInEBook(paretnId, chapterId, null);
		request.setAttribute("nextChapter", enhancedNextEBookChapter);

		statisticsService.updateCount(ItemTypeEnumForSource.BOOK_CHAPTER, chapterId, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

		return "web/book/qianlongdazangjing/info";
	}

	// qldzj/catalog-2/20.html

}
